package com.sxpcwlkj.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Base64;

/**
 * 文件工具类
 * @author: xijue
 * @date: 2020/12/14 14:44
 */
public class FileImgUtils extends cn.hutool.core.io.FileUtil {

    public static String fileUrlToBase64(String fileUrl){
        try {
            URL url = new URL(fileUrl);
            InputStream inputStream = url.openStream();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            byte[] imageBytes = outputStream.toByteArray();
            String base64String = Base64.getEncoder().encodeToString(imageBytes);
            inputStream.close();
            outputStream.close();
            return base64String;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
